import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["label", "size", "disabled", "onChange", "className", "style", "children", "valuePropName", "placeholder", "labelFormatter", "bordered", "footerRender", "allowClear", "otherFieldProps", "valueType", "placement"];
import { jsx as _jsx } from "react/jsx-runtime";
import { dateArrayFormatter, dateFormatterMap, FieldLabel, FilterDropdown, useMountMergeState } from '@ant-design/pro-utils';
import { ConfigProvider } from 'antd-v4';
import classNames from 'classnames';
import React, { useContext, useMemo, useState } from 'react';
import './index.less';

var LightWrapper = function LightWrapper(props) {
  var _objectSpread2;

  var label = props.label,
      size = props.size,
      disabled = props.disabled,
      propsOnChange = props.onChange,
      className = props.className,
      style = props.style,
      children = props.children,
      valuePropName = props.valuePropName,
      placeholder = props.placeholder,
      labelFormatter = props.labelFormatter,
      bordered = props.bordered,
      footerRender = props.footerRender,
      allowClear = props.allowClear,
      otherFieldProps = props.otherFieldProps,
      valueType = props.valueType,
      placement = props.placement,
      rest = _objectWithoutProperties(props, _excluded);

  var _useContext = useContext(ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = getPrefixCls('pro-field-light-wrapper');

  var _useState = useState(props[valuePropName]),
      _useState2 = _slicedToArray(_useState, 2),
      tempValue = _useState2[0],
      setTempValue = _useState2[1];

  var _useMountMergeState = useMountMergeState(false),
      _useMountMergeState2 = _slicedToArray(_useMountMergeState, 2),
      open = _useMountMergeState2[0],
      setOpen = _useMountMergeState2[1];

  var onChange = function onChange() {
    var _otherFieldProps$onCh;

    for (var _len = arguments.length, restParams = new Array(_len), _key = 0; _key < _len; _key++) {
      restParams[_key] = arguments[_key];
    }

    otherFieldProps === null || otherFieldProps === void 0 ? void 0 : (_otherFieldProps$onCh = otherFieldProps.onChange) === null || _otherFieldProps$onCh === void 0 ? void 0 : _otherFieldProps$onCh.call.apply(_otherFieldProps$onCh, [otherFieldProps].concat(restParams));
    propsOnChange === null || propsOnChange === void 0 ? void 0 : propsOnChange.apply(void 0, restParams);
  };

  var labelValue = props[valuePropName];
  /** DataRange的转化，moment 的 toString 有点不好用 */

  var labelText = useMemo(function () {
    var _valueType$toLowerCas;

    if ((valueType === null || valueType === void 0 ? void 0 : (_valueType$toLowerCas = valueType.toLowerCase()) === null || _valueType$toLowerCas === void 0 ? void 0 : _valueType$toLowerCas.endsWith('range')) && !labelFormatter) {
      return dateArrayFormatter(labelValue, dateFormatterMap[valueType] || 'YYYY-MM-DD');
    }

    return labelValue;
  }, [labelValue, valueType, labelFormatter]);
  return _jsx(FilterDropdown, {
    disabled: disabled,
    onVisibleChange: setOpen,
    placement: placement,
    visible: open,
    label: _jsx(FieldLabel, {
      ellipsis: true,
      size: size,
      onClear: function onClear() {
        onChange === null || onChange === void 0 ? void 0 : onChange();
        setTempValue(undefined);
      },
      bordered: bordered,
      style: style,
      className: className,
      label: label,
      placeholder: placeholder,
      value: labelText,
      disabled: disabled,
      expanded: open,
      formatter: labelFormatter,
      allowClear: allowClear
    }),
    footer: {
      onClear: function onClear() {
        return setTempValue(undefined);
      },
      onConfirm: function onConfirm() {
        onChange === null || onChange === void 0 ? void 0 : onChange(tempValue);
        setOpen(false);
      }
    },
    footerRender: footerRender,
    children: _jsx("div", {
      className: classNames("".concat(prefixCls, "-container"), className),
      style: style,
      children: /*#__PURE__*/React.cloneElement(children, _objectSpread(_objectSpread({}, rest), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, valuePropName, tempValue), _defineProperty(_objectSpread2, "onChange", function onChange(e) {
        setTempValue((e === null || e === void 0 ? void 0 : e.target) ? e.target.value : e);
      }), _objectSpread2), children.props))
    })
  });
};

export { LightWrapper };